package com.example.questionbase.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author fan
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
//@ApiModel("返回结果")
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -3826891916021780628L;
//    @ApiModelProperty("编码")
    private Integer code;
//    @ApiModelProperty("信息")
    private String info;
//    @ApiModelProperty("数据")
    private T data;


    public static Result buildResult(Constants.ResponseCode code) {
        return new Result(code.getCode(), code.getInfo());
    }

    public static Result buildResult(Constants.ResponseCode code, String info) {
        return new Result(code.getCode(), info);
    }

    public static Result buildResult(Integer code, String info) {
        return new Result(code, info);
    }

    public static Result buildResult(Constants.ResponseCode code, Constants.ResponseCode info) {
        return new Result(code.getCode(), info.getInfo());
    }

    public static Result buildSuccessResult() {
        return new Result(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo());
    }

    /**
     * 成功+返回数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> buildSuccessResultWithData(T data){
        return new Result<T>(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo(),data);
    }

    public static Result buildErrorResult() {
        return new Result(Constants.ResponseCode.UN_ERROR.getCode(), Constants.ResponseCode.UN_ERROR.getInfo());
    }

    public static Result buildErrorResult(String info) {
        return new Result(Constants.ResponseCode.UN_ERROR.getCode(), info);
    }


    public Result(Integer code, String info) {
        this.code = code;
        this.info = info;
    }

}
